package com.cskaoyan.config.shiro;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @Description: 解决跨域时sessionId改变的问题
 * @Author: WD created on 2021-09-19 15:01
 * @Version:
 */
public class MarketSessionManager extends DefaultWebSessionManager {
    @Value("${Info.admin_token}")
    private String admin_token;
    @Value("${Info.wx_token}")
    private String wx_token;

    @Override
    protected Serializable getSessionId(ServletRequest servletRequest, ServletResponse servletResponse) {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String sessionId = request.getHeader(admin_token);
        if(sessionId != null && !"".equals(sessionId)){
            return sessionId;
        }
        String sessionId2 = request.getHeader(wx_token);
        if(sessionId2 != null && !"".equals(sessionId2)){
            return sessionId2;
        }

        return super.getSessionId(request, servletResponse);
    }
}
